﻿using System;
using System.Diagnostics.Contracts;

namespace Wombat.Core.Ports
{
    public class GenericObservablePort: IObservablePort
    {
        private readonly ITagValueObservable _observableHost;

        internal GenericObservablePort(string portName, PortBehavior portBehavior, ITagValueObservable observableHost)
        {
            Contract.Assert(observableHost is IUniqueIdentifiable && ((IUniqueIdentifiable)observableHost).Guid != null &&
                ((IUniqueIdentifiable)observableHost).Guid != Guid.Empty);
            _observableHost = observableHost;
            Name = portName;
            Guid = ((IUniqueIdentifiable)observableHost).Guid;
            PortBehavior = portBehavior;
        }

        public string Name { get; private set; }
        public Guid Guid { get; private set; }
        public PortBehavior PortBehavior { get; private set; }
        public IDisposable Subscribe<T>(string pattern, IObserver<T> observer) where T : ITagValue
        {
            return _observableHost.Subscribe(pattern, observer);
        }
    }
}